package com.base.library.coroutine

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import kotlin.coroutines.CoroutineContext

private val defaultScope: DefaultScope by lazy { DefaultScope() }
private val defaultSupervisorScope: DefaultSupervisorScope by lazy { DefaultSupervisorScope() }

private class DefaultSupervisorScope(override val coroutineContext: CoroutineContext = Dispatchers.Default + SupervisorJob()) :
    CoroutineScope

private class DefaultScope(override val coroutineContext: CoroutineContext = Dispatchers.Default) :
    CoroutineScope

/**
 * start defaultSupervisor coroutine 子协程的异常由子协程自己处理
 */
fun defaultSupervisor(block: suspend CoroutineScope.() -> Unit) =
    defaultSupervisorScope.launch {
        block()
    }

/**
 * start defaultSupervisor coroutine 子协程的异常由父协程处理
 */
fun default(block: suspend CoroutineScope.() -> Unit) =
    defaultScope.launch {
        block()
    }